package com.feicheng.lanniao.controller.pc;
import com.feicheng.lanniao.entity.HouseImage;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.*;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author tianchunlei
 * @Des 前台用户房屋小图接口
 * @Date 2021/9/27
 **/
@RestController
@RequestMapping("/lanniao-user/house")
public class HouseImagePcController {

    @Resource
    private HouseImageService houseImageService;

    @Resource
    private IHouseSellService iHouseSellService;

    @Resource
    private IHouseLeaseService iHouseLeaseService;

    /**
     * @Author tianchunlei
     * @Description 批量上传小图接口
     * @Date 2021/9/26 15:33
     * @Param images 多张图片拼接的地址
     * @Param sellAndLeaseId 卖房id或租房id
     * @param type 类型 1卖房 0租房
     * @return
     */
    @PutMapping("/image/upload")
    public ResultInfo carImageUpload(
            Integer sellAndLeaseId,
            Integer type,
            String images){
        //解析多张图片的字符串
        String[] imageUrls = images.split(",");
        List<HouseImage> houseImageUrlList = new ArrayList<>();
        Integer id = null;
        if (type == 1) {
            id = iHouseSellService.getById(sellAndLeaseId).getHouseId();
        }

        if (type == 0) {
            id = iHouseLeaseService.getById(sellAndLeaseId).getHouseId();
        }

        for (String houseImageUrl : imageUrls) {
            HouseImage houseImage = new HouseImage(id,houseImageUrl);
            houseImageUrlList.add(houseImage);
        }

        System.out.println(houseImageUrlList);
        return houseImageService.batchAdd(houseImageUrlList);
    }

    /**
     * @Author tianchunlei
     * @Description //批量删除小图片地址的接口
     * @Date 2021/9/26 16:48
     * @Param houseAndLeaseId 买房或租房id
     * @Param  type type 类型 1。卖房  0租房
     * @return
     */
    @DeleteMapping("/image/remove")
    public ResultInfo deleteImageUrl(Integer houseAndLeaseId, Integer type){
        Integer id = null;
        if (type == 1) {
            id  = iHouseSellService.getById(houseAndLeaseId).getHouseId();
        }

        if (type == 0) {
            id = iHouseLeaseService.getById(houseAndLeaseId).getHouseId();
        }

        return houseImageService.batchDelete(id);
    }

    /**
     * @Author tianchunlei
     * @Description //按照房屋编号查询车辆小图集合
     * @Date 2021/9/26 16:57
     * @Param houseAndLeaseId 买房或租房id
     * type type 类型 1。卖房  0租房
     * @return
     */
    @GetMapping("/image/list")
    public ResultInfo list(Integer houseAndLeaseId, Integer type){
        Integer id = null;
        if (type == 1) {
            id = iHouseSellService.getById(houseAndLeaseId).getHouseId();
        }
        if (type == 0) {
            id = iHouseLeaseService.getById(houseAndLeaseId).getHouseId();
        }
        return houseImageService.selectUrlAll(id);
    }
}
